package com.lam.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.dormitory.entity.Dormitory;
import com.lam.dormitory.entity.DormitoryService;
import com.lam.dormitory.mapper.DormitoryMapper;
import com.lam.dormitory.mapper.DormitoryServiceMapper;
import com.lam.dormitory.service.IDormitoryServiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lam.dormitory.util.ResponseTemplate;
import com.lam.dormitory.vo.ChartHygieneVo;
import com.lam.dormitory.vo.ChartVo;
import com.lam.dormitory.vo.DormitoryServiceDetailVo;
import com.lam.dormitory.vo.DormitoryServiceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lam
 * @since 2019-12-26
 */
@Service
public class DormitoryServiceServiceImpl extends ServiceImpl<DormitoryServiceMapper, DormitoryService> implements IDormitoryServiceService {
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Autowired
    private DormitoryServiceMapper dormitoryServiceMapper;

    @Override
    public List<DormitoryServiceVo> selectDsVoByDorId(Long dorId) {
        return this.baseMapper.selectDsVoByDorId(dorId);
    }

    @Override
    public List<DormitoryServiceVo> selectDsVo(DormitoryService dormitoryService) {
        return this.baseMapper.selectDsVo(dormitoryService);
    }

    @Override
    public List<DormitoryServiceDetailVo> selectDsDetailVo(DormitoryService dormitoryService) {
        return this.baseMapper.selectDsDetailVo(dormitoryService);
    }

    @Override
    public DormitoryService selectByBuildingNoAndDorNoAndMonth(Date dsMonth, Integer buildingNo, Integer dorNo) {
        return this.baseMapper.selectByBuildingNoAndDorNoAndMonth(dsMonth, buildingNo, dorNo);
    }

    @Override
    public ResponseTemplate saveBatch(List<DormitoryServiceVo> list) {
        List<DormitoryService> dormitoryServiceList = new ArrayList<>();
        for (DormitoryServiceVo dormitoryServiceVo : list) {
            DormitoryService dormitoryServiceOne = this.baseMapper.selectByBuildingNoAndDorNoAndMonth(dormitoryServiceVo.getDsMonth(), dormitoryServiceVo.getBuildingNo(), dormitoryServiceVo.getDorNo());
            Dormitory dormitory = dormitoryMapper.selectDorIdByBuildingNoAndDorNo(dormitoryServiceVo.getBuildingNo(), dormitoryServiceVo.getDorNo());
            if (dormitoryServiceOne == null && dormitory != null) {
                DormitoryService dormitoryService = new DormitoryService();
                dormitoryService.setDorId(dormitory.getDorId());
                dormitoryService.setDsElectricBill(dormitoryServiceVo.getDsElectricBill());
                dormitoryService.setDsElectricBillDetail(dormitoryServiceVo.getDsElectricBillDetail());
                dormitoryService.setDsWaterBill(dormitoryServiceVo.getDsWaterBill());
                dormitoryService.setDsWaterBillDetail(dormitoryServiceVo.getDsWaterBillDetail());
                dormitoryService.setDsInternetBill(dormitoryServiceVo.getDsInternetBill());
                dormitoryService.setDsInternetBillDetail(dormitoryServiceVo.getDsInternetBillDetail());
                dormitoryService.setDsDorHygiene(dormitoryServiceVo.getDsDorHygiene());
                dormitoryService.setDsDorHygieneDetail(dormitoryServiceVo.getDsDorHygieneDetail());
                dormitoryService.setDsMonth(dormitoryServiceVo.getDsMonth());
                dormitoryService.setStatus(1);
                dormitoryServiceList.add(dormitoryService);
            }
        }
        boolean b = this.saveBatch(dormitoryServiceList);
        if (b) {
            return new ResponseTemplate(200, "添加成功");
        }
        return new ResponseTemplate(500, "添加失败");
    }

    @Override
    public List<ChartVo> monthTotalChart() {
        return this.baseMapper.monthTotalChart();
    }

    @Override
    public List<ChartVo> monthBuildingTotalChart(DormitoryService dormitoryService) {
        return this.baseMapper.monthBuildingTotalChart(dormitoryService);
    }

    @Override
    public List<DormitoryService> selectBuildingNo() {
        return this.baseMapper.selectMonths();
    }

    @Override
    public List<ChartVo> monthBuildingDorNoTotal(DormitoryService dormitoryService) {
        return this.baseMapper.monthBuildingDorNoTotal(dormitoryService);
    }

    @Override
    public List<ChartHygieneVo> monthHygieneTotal(DormitoryService dormitoryService) {
        return this.baseMapper.monthHygieneTotal(dormitoryService);
    }

    @Override
    public List<ChartHygieneVo> monthBuildingDorNoHygieneTotal(DormitoryService dormitoryService) {
        return this.baseMapper.monthBuildingDorNoHygieneTotal(dormitoryService);
    }
}
